package cn.t.persistence.mapper.user;

import cn.t.enums.user.RoleTypeEnum;
import cn.t.model.user.ParticipantRoleDO;
import cn.t.persistence.entity.user.ParticipantRolePO;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 参与者和角色关联表领域对象与实体对象转换
 *
 * @author l
 */

public interface ParticipantRoleMapper extends BaseMapper<ParticipantRolePO> {


    /**
     * 根据 参与者号获取 菜单类权限
     *
     * @param participantId 参与者号
     * @see RoleTypeEnum
     * ANONYMOUS(10, "个人匿名角色"), SYSTEM(11, "系统初始化角色")
     */
    @Select("select pr.* from t_participant_role pr left join t_role_info ri on pr.role_id = ri.id " +
            "where pr.participant_id = #{participantId} " +
            "and ri.role_type in (10, 11)" +
            "and pr.is_deleted = 'N' and pr.status = 1 and ri.is_deleted = 'N' and ri.status = 1")
    List<ParticipantRoleDO> findMenuByParticipantId(Long participantId);

    @Select("select pr.* from t_participant_role pr left join t_role_info ri on pr.role_id = ri.id " +
            "where pr.participant_id = #{participantId} " +
            "and ri.role_type in (10, 11)")
    List<ParticipantRoleDO> findMenuByParticipantIdIgnoreDisable(Long participantId);


    @Select("select pr.* from t_participant_role pr left join t_role_info ri on pr.role_id = ri.id " +
            "where pr.participant_id = #{participantId} " +
            "and ri.role_type in (20, 21)" +
            "and pr.is_deleted = 'N' and pr.status = 1 and ri.is_deleted = 'N' and ri.status = 1")
    List<ParticipantRoleDO> findMetalIssueByParticipantId(Long participantId);

    @Select("SELECT DISTINCT ri.email_address FROM t_participant_info p" +
            " JOIN t_participant_role s ON p.id = s.participant_id " +
            "LEFT JOIN t_role_info ri ON s.role_id = ri.id " +
            "WHERE p.firm_id = #{firmId}  and  p.is_deleted = 'N' " +
            "AND ri.`status` = 1 AND ri.role_type IN( 20, 21)  and  ri.is_deleted = 'N'" +
            "AND s.participant_id = #{participantId}  and  s.is_deleted = 'N' and ri.email_address!='' ")
    List<String> findEmailAddressByParticipantId(Long participantId, Long firmId);


    @Delete("delete from t_participant_role t where t.role_id = #{roleId} ")
    Boolean deleteByRoleId(Long roleId);

    @Delete("update t_participant_role  set is_deleted='Y' where id IN (SELECT pri.id FROM  (SELECT pr.id  FROM  t_participant_role pr " +
            "              JOIN t_participant_info pi ON pr.participant_id = pi.id  " +
            "            WHERE  pr.role_id = #{roleId} AND pi.firm_id = #{firmId} )AS pri ) ")
    Boolean deleteParticipantRoleByRoleId(Long roleId, Long firmId);

    @Delete("delete from t_participant_role t where t.id = #{id} ")
    boolean removeById(Long id);

    @Delete("delete from t_participant_role t where t.participant_id = #{participantId} " +
            "and exists (select 1 from t_role_info ri where ri.id = t.role_id)")
    Boolean removeByParticipantId(Long participantId);

    @Select("select pr.* from t_participant_role pr left join t_role_info ri on pr.role_id = ri.id " +
            "where pr.participant_id = #{participantId} " +
            "and ri.role_type in (31)" +
            "and pr.is_deleted = 'N' and pr.status = 1 and ri.is_deleted = 'N' and ri.status = 1")
    List<ParticipantRoleDO> findMetalReplenishByParticipantId(Long participantId);

    @Select("select pr.* from t_participant_role pr left join t_role_info ri on pr.role_id = ri.id " +
            "where pr.participant_id = #{participantId} ")
    List<ParticipantRoleDO> findAllByParticipantIdIgnoreDisable(Long participantId);

    @Select("select pr.* from t_participant_role pr left join t_role_info ri on pr.role_id = ri.id " +
            "where pr.participant_id = #{participantId} " +
            "and pr.is_deleted = 'N' and pr.status = 1 and ri.is_deleted = 'N' and ri.status = 1")
    List<ParticipantRoleDO> findAllByParticipantId(Long participantId);
}
